package com.note.chh.notebook.UI.enjoy;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.note.chh.notebook.Api.Constant;
import com.note.chh.notebook.R;
import com.note.chh.notebook.UI.base.BaseActivity;
import com.note.chh.notebook.Uitls.FileUtils;
import com.note.chh.notebook.Widget.doodleView.DoodleView;
import com.qmuiteam.qmui.alpha.QMUIAlphaImageButton;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.popup.QMUIListPopup;
import com.qmuiteam.qmui.widget.popup.QMUIPopup;

import java.io.File;

import butterknife.BindView;

//绘画板
public class DoodleViewActivity extends BaseActivity {


    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.mDoodle_view)
    DoodleView dvView;
    private QMUIAlphaImageButton rightImageButton;
    private AlertDialog mColorDialog;
    private AlertDialog mPaintDialog;
    private AlertDialog mShapeDialog;

    @Override
    protected int bindLayout() {
        return R.layout.activity_doodle_view;
    }

    @Override
    public void initTopBar() {
        super.initTopBar();
        topbar.setBackgroundColor(getResources().getColor(R.color.app_color_blue));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        topbar.setTitle(getResources().getString(R.string.doodle_view));
        rightImageButton = topbar.addRightImageButton(R.mipmap.more, R.id.top_bar_right_doodle);
        rightImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMenuPop();
            }
        });
    }
    private QMUIListPopup listPopu;
    private void showMenuPop() {
        final String[] items = new String[]{"画笔颜色", "画笔粗细", "画笔形状","重新绘制","保存图片"};
//        new QMUIDialog.MenuDialogBuilder(this)
//                .addItems(items, new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//
//                        dialog.dismiss();
//                    }
//                })
//                .show();

        ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.simple_list_item, items);

        listPopu = new QMUIListPopup(this, QMUIPopup.DIRECTION_NONE, adapter);
        listPopu.create(QMUIDisplayHelper.dp2px(this, 150), ViewGroup.LayoutParams.WRAP_CONTENT, new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                switch (i){
                    case 0:
                        showColorDialog();
                        break;
                    case 1:
                        showSizeDialog();
                        break;
                    case 2:
                        showShapeDialog();
                        break;
                    case 3:
                        dvView.reset();
                        break;
                    case 4:
//                        File file = new File(FileUtils.getSavePath(Constant.drawImageSaveFolder+"/"+System.currentTimeMillis()+".png"));
//                        if (!file.exists()) {
//                            dvView.savePicByPNG(dvView.getBitmap(), FileUtils.getSavePath(Constant.drawImageSaveFolder+"/"+System.currentTimeMillis()+".png"));
                            ImageUtils.save(dvView.getBitmap(),FileUtils.getSavePath(Constant.drawImageSaveFolder+"/"+System.currentTimeMillis()+".png"), Bitmap.CompressFormat.PNG);
                            Toast.makeText(DoodleViewActivity.this,"图片已保存",Toast.LENGTH_SHORT).show();
//                        }
                        break;
                }
                listPopu.dismiss();
            }
        });
        listPopu.setPositionOffsetYWhenTop(50);
        listPopu.setAnimStyle(QMUIPopup.ANIM_GROW_FROM_CENTER);
        listPopu.setPreferredDirection(QMUIPopup.DIRECTION_NONE);
        listPopu.show(rightImageButton);
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    /**
     * 显示选择画笔颜色的对话框
     */
    private void showColorDialog() {
        if (mColorDialog == null) {
            mColorDialog = new AlertDialog.Builder(this)
                    .setTitle("选择颜色")
                    .setSingleChoiceItems(new String[]{"蓝色", "红色", "黑色"}, 0,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    switch (which) {
                                        case 0:
                                            dvView.setColor("#0000ff");
                                            break;
                                        case 1:
                                            dvView.setColor("#ff0000");
                                            break;
                                        case 2:
                                            dvView.setColor("#272822");
                                            break;
                                        default:
                                            break;
                                    }
                                    dialog.dismiss();
                                }
                            }).create();
        }
        mColorDialog.show();
    }

    /**
     * 显示选择画笔粗细的对话框
     */
    private void showSizeDialog() {
        if (mPaintDialog == null) {
            mPaintDialog = new AlertDialog.Builder(this)
                    .setTitle("选择画笔粗细")
                    .setSingleChoiceItems(new String[]{"细", "中", "粗"}, 0,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    switch (which) {
                                        case 0:
                                            dvView.setSize(SizeUtils.dp2px(5));
                                            break;
                                        case 1:
                                            dvView.setSize(SizeUtils.dp2px(10));
                                            break;
                                        case 2:
                                            dvView.setSize(SizeUtils.dp2px(15));
                                            break;
                                        default:
                                            break;
                                    }
                                    dialog.dismiss();
                                }
                            }).create();
        }
        mPaintDialog.show();
    }

    /**
     * 显示选择画笔形状的对话框
     */
    private void showShapeDialog() {
        if (mShapeDialog == null) {
            mShapeDialog = new AlertDialog.Builder(this)
                    .setTitle("选择形状")
                    .setSingleChoiceItems(new String[]{"路径", "直线", "矩形", "圆形", "实心矩形", "实心圆"}, 0,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    switch (which) {
                                        case 0:
                                            dvView.setType(DoodleView.ActionType.Path);
                                            break;
                                        case 1:
                                            dvView.setType(DoodleView.ActionType.Line);
                                            break;
                                        case 2:
                                            dvView.setType(DoodleView.ActionType.Rect);
                                            break;
                                        case 3:
                                            dvView.setType(DoodleView.ActionType.Circle);
                                            break;
                                        case 4:
                                            dvView.setType(DoodleView.ActionType.FillEcRect);
                                            break;
                                        case 5:
                                            dvView.setType(DoodleView.ActionType.FilledCircle);
                                            break;
                                        default:
                                            break;
                                    }
                                    dialog.dismiss();
                                }
                            }).create();
        }
        mShapeDialog.show();
    }


}
